package com.bdmc.alarm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.alarm.domain.bo.AlarmEventBo;
import com.bdmc.alarm.domain.vo.AlarmEventVo;
import com.bdmc.alarm.domain.AlarmEvent;
import com.bdmc.alarm.mapper.AlarmEventMapper;
import com.bdmc.alarm.service.IAlarmEventService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 告警事件Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-10
 */
@Service
public class AlarmEventServiceImpl extends ServicePlusImpl<AlarmEventMapper, AlarmEvent, AlarmEventVo> implements IAlarmEventService {

    @Override
    public AlarmEventVo queryById(Long eventId){
        List<AlarmEventVo> alarmEventVos = queryList(new AlarmEventBo().setEventId(eventId));
        if(alarmEventVos.size()>0)
            return alarmEventVos.get(0);
        return null;
    }

    @Override
    public TableDataInfo<AlarmEventVo> queryPageList(AlarmEventBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AlarmEvent> lqw = buildQueryWrapper(bo);
        Page<AlarmEventVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<AlarmEventVo> mySelectPage(AlarmEventBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<AlarmEventVo> queryList(AlarmEventBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<AlarmEvent> buildQueryWrapper(AlarmEventBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AlarmEvent> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTargetType()), AlarmEvent::getTargetType, bo.getTargetType());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmType()), AlarmEvent::getAlarmType, bo.getAlarmType());
        lqw.eq(bo.getCarId() != null, AlarmEvent::getCarId, bo.getCarId());
        lqw.eq(bo.getPersonId() != null, AlarmEvent::getPersonId, bo.getPersonId());
        lqw.eq(bo.getWorkPlanId() != null, AlarmEvent::getWorkPlanId, bo.getWorkPlanId());
        lqw.eq(bo.getWorkTeamId() != null, AlarmEvent::getWorkTeamId, bo.getWorkTeamId());
        lqw.eq(bo.getEventStart() != null, AlarmEvent::getEventStart, bo.getEventStart());
        lqw.eq(bo.getEventEnd() != null, AlarmEvent::getEventEnd, bo.getEventEnd());
        lqw.eq(bo.getEventFinish() != null, AlarmEvent::getEventFinish, bo.getEventFinish());
        lqw.eq(StringUtils.isNotBlank(bo.getEventDesc()), AlarmEvent::getEventDesc, bo.getEventDesc());
        lqw.eq(bo.getPersistTime() != null, AlarmEvent::getPersistTime, bo.getPersistTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(AlarmEventBo bo) {
        AlarmEvent add = BeanUtil.toBean(bo, AlarmEvent.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setEventId(add.getEventId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(AlarmEventBo bo) {
        AlarmEvent update = BeanUtil.toBean(bo, AlarmEvent.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(AlarmEvent entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
